import Leap, cv2
from supportFunctions import *
from time import sleep
import numpy as np

# 创建 Leap Motion 控制器对象
controller = Leap.Controller()
# 允许访问原始图像数据
controller.set_policy(Leap.Controller.POLICY_IMAGES)

# 等待一段时间以确保控制器初始化完成
sleep(0.1)

# 获取当前帧和左侧图像
frame = controller.frame()
image = frame.images[0]

# 获取左右图像的畸变矫正参数
left_coordinates, left_coefficients = convert_distortion_maps(frame.images[0])
right_coordinates, right_coefficients = convert_distortion_maps(frame.images[1])

# 将图像数据封装为 NumPy 数组
i_address = int(image.data_pointer)
ctype_array_def = ctypes.c_ubyte * image.height * image.width
as_ctype_array = ctype_array_def.from_address(i_address)
as_numpy_array = np.ctypeslib.as_array(as_ctype_array)
rawImage = np.reshape(as_numpy_array, (image.height, image.width))

# 计算手指末端和基部的坐标
end_horizontal_slope = -1 * (frame.hands[0].fingers[1].tip_position.x - 20) / frame.hands[0].fingers[1].tip_position.y
end_vertical_slope = frame.hands[0].fingers[1].tip_position.z / frame.hands[0].fingers[1].tip_position.y

start_horizontal_slope = -1 * (frame.hands[0].fingers[1].tip_position.x - 20) / frame.hands[0].fingers[1].tip_position.y
start_vertical_slope = frame.hands[0].fingers[1].tip_position.z / frame.hands[0].fingers[1].tip_position.y

# 将坐标映射到图像坐标
endPixel = image.warp(Leap.Vector(end_horizontal_slope, end_vertical_slope, 0))
startPixel = image.warp(Leap.Vector(start_horizontal_slope, start_vertical_slope, 0))

# 将映射的坐标转换为图像索引
endPixelIndices = [math.floor(endPixel.y), math.floor(endPixel.x)]
startPixelIndices = [math.floor(startPixel.y), math.floor(startPixel.x)]

# 创建手指区域的掩码
fingerMask = np.zeros((image.height, image.width))
fingerMask[startPixelIndices[0], startPixelIndices[1]] = 255
fingerMask[endPixelIndices[0], endPixelIndices[1]] = 255

# 使用畸变矫正参数将手指区域的掩码映射到目标图像
mappedMask = cv2.remap(fingerMask, left_coordinates, left_coefficients, interpolation=cv2.INTER_LINEAR)
mappedMask = cv2.resize(mappedMask, (400, 400), 0, 0, cv2.INTER_LINEAR)

# 使用畸变矫正参数将原始图像映射到目标图像
grayScaleImage = cv2.remap(rawImage, left_coordinates, left_coefficients, interpolation=cv2.INTER_LINEAR)
grayScaleImage = cv2.resize(grayScaleImage, (400, 400), 0, 0, cv2.INTER_LINEAR)

# 保存映射后的灰度图像
cv2.imwrite('grayScaleImage.png', grayScaleImage)
